package edu.xidian.bdilab.jrbigdata.service.enterprise.register.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.xidian.bdilab.jrbigdata.controller.enterprise.EnterpriseCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.EnterpriseInfo;
import edu.xidian.bdilab.jrbigdata.domain.EnterpriseUser;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseInfoMapper;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseUserMapper;
import edu.xidian.bdilab.jrbigdata.service.enterprise.register.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @ClassName RegisterServiceImpl
 * @Description TODO
 * @Author tuantuan
 * @Date 2019/3/25 上午9:31
 * @Version 1.0
 * @Attention Copyright (C), 2004-2019, BDILab, XiDian University
 **/
@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    EnterpriseInfoMapper enterpriseInfoMapper;
    @Autowired
    EnterpriseUserMapper enterpriseUserMapper;

    /**
     * 查询用户是否已经注册
     * @param socialCode 社会信用号，主键
     * @description 原意指查不到的时候插入，现在已经修改
     * @Date 05/26
     * @return 查到了返回false，没查到返回true
     */
    @Override
    public boolean checkEnterpriseInfo(String socialCode) {
        QueryWrapper<EnterpriseInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("social_code",socialCode);

        EnterpriseInfo enterpriseInfo = enterpriseInfoMapper.selectOne(queryWrapper);
        if (enterpriseInfo == null){ return true; }
        return false;
    }

    /**
     * 将企业信息插入表中
     * @return
     */
    @Override
    public void setEnterpriseInfo(String socialCode, String enterpriseName,
                                  int registeredCapital, String ownerName,
                                  int region, String street,
                                  String ownerPhone, String contactName,
                                  String contactPhone, Date establishTime,
                                  String establishAddress, Integer fundQuota,
                                  Integer available, String businessScop,
                                  String enterpriseDesc, String accountBank,
                                  String electricityChargeCode, String socialSecurityCode) {

        EnterpriseInfo enterpriseInfo = new EnterpriseInfo();

        enterpriseInfo.setSocialCode(socialCode);
        enterpriseInfo.setEnterpriseName(enterpriseName);
        enterpriseInfo.setRegisteredCaptial(registeredCapital);
        enterpriseInfo.setOwnerName(ownerName);
        enterpriseInfo.setRegion(region);
        enterpriseInfo.setStreet(street);
        enterpriseInfo.setOwnerPhone(ownerPhone);
        enterpriseInfo.setContactName(contactName);
        enterpriseInfo.setContactPhone(contactPhone);
        enterpriseInfo.setEstablishTime(establishTime);
        enterpriseInfo.setEstablishAddress(establishAddress);
        if (fundQuota != null ){ enterpriseInfo.setFundQuota(fundQuota); }
        if( available != null){ enterpriseInfo.setAvailable(available); }
        enterpriseInfo.setBusinessScop(businessScop);
        if( enterpriseDesc != null&&enterpriseDesc.length()!=0){
            enterpriseInfo.setEnterpriseDesc(enterpriseDesc);
        }
        else{
            enterpriseInfo.setEnterpriseDesc("该企业暂无详情");
        }
        if (accountBank != null ){ enterpriseInfo.setAccountBank(accountBank); }
        if (electricityChargeCode != null ){ enterpriseInfo.setElectricityChargeCode(electricityChargeCode); }
        if (socialSecurityCode != null ){ enterpriseInfo.setSocialSecurityCode(socialSecurityCode); }

        enterpriseInfoMapper.insert(enterpriseInfo);
    }

    /**
     * 将注册企业添加至企业用户表中
     */
    @Override
    public void addEnterpriseUser(String socialCode, String md5Password) {
        EnterpriseUser enterpriseUser = new EnterpriseUser();
        enterpriseUser.setUserName(socialCode);
        enterpriseUser.setPassword(md5Password);
        enterpriseUserMapper.insert(enterpriseUser);
    }
}
